<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="优惠券名称" label-width="90px" prop="couponName">
        <el-input
          v-model="queryParams.couponName"
          placeholder="请输入优惠券名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" label-width="50px" prop="couponStatus">
        <el-select
          v-model="queryParams.couponStatus"
          placeholder="优惠券状态"
          clearable
          size="small"
        >
          <el-option
            v-for="item in dict.type.coupon_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类别" label-width="50px" prop="category">
        <el-select
          v-model="queryParams.category"
          placeholder="优惠券类别"
          clearable
          size="small"
        >
          <el-option
            v-for="item in dict.type.coupon_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          v-hasPermi="['coupon:coupon:query']"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['coupon:coupon:add']"
          >新增优惠券</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple.length === 0"
          @click="handleDelete(null)"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['coupon:coupon:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="seckillList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column
        type="selection"
        width="55"
        align="center"
      /> -->
      <el-table-column
        min-width="140"
        label="优惠券名称"
        align="center"
        prop="couponName"
        show-overflow-tooltip
      />
      <el-table-column
        min-width="120"
        label="优惠券类别"
        align="center"
        prop="category"
      >
        <template slot-scope="scope">
          {{ scope.row.category | typeFilter(dict.type.coupon_type) }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="120"
        label="优惠券状态"
        align="center"
        prop="couponStatus"
      >
        <template slot-scope="scope">
          {{ scope.row.couponStatus | typeFilter(dict.type.coupon_status) }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="优惠信息"
        align="center"
        prop="preferentialInfo"
      />
      <el-table-column
        min-width="300"
        label="使用有效期"
        align="center"
        prop="useTime"
      />
      <el-table-column
        min-width="140"
        label="是否允许转赠"
        align="center"
        prop="isTurn"
        sortable
      >
        <template slot-scope="scope">
          {{ scope.row.isTurn | typeFilter(dict.type.coupon_is_turn) }}
        </template>
      </el-table-column>
      <el-table-column
        min-width="160"
        label="优惠券开始时间"
        align="center"
        prop="sendTimeStart"
        sortable
      />
      <el-table-column
        min-width="160"
        label="优惠券结束时间"
        align="center"
        prop="sendTimeEnd"
        sortable
      />
      <el-table-column
        min-width="140"
        label="所属租户"
        align="center"
        prop="tenantName"
      />
      <el-table-column
        min-width="160"
        label="创建时间"
        align="center"
        prop="createTime"
        sortable
      />
      <el-table-column
        min-width="140"
        label="创建人"
        align="center"
        prop="userName"
      />

      <el-table-column
        min-width="140"
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handDetail(scope.row)"
            >详情</el-button
          >
          <!-- 1 -->
          <el-dropdown
            v-if="scope.row.couponStatus === 1"
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
          >
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleSuspend"
                icon="el-icon-key"
                v-hasPermi="['coupon:coupon:suspend']"
                >暂停</el-dropdown-item
              >
              <el-dropdown-item
                command="appointBusinss"
                icon="el-icon-key"
                v-hasPermi="['coupon:coupon:appoint']"
                >指定业务</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
          <!-- 2 -->
          <el-dropdown
            v-else-if="scope.row.couponStatus === 2"
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
          >
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleEdit"
                icon="el-icon-key"
                v-hasPermi="['coupon:coupon:edit']"
                >编辑</el-dropdown-item
              >
              <el-dropdown-item
                command="handleDelete"
                icon="el-icon-key"
                v-hasPermi="['coupon:coupon:remove']"
                >删除</el-dropdown-item
              >
              <el-dropdown-item
                command="handleStart"
                icon="el-icon-key"
                v-hasPermi="['coupon:coupon:ground']"
                >上架</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
          <!-- 3 -->
          <el-dropdown
            v-else-if="scope.row.couponStatus === 3"
            size="mini"
            @command="(command) => handleCommand(command, scope.row)"
          >
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="handleOpen"
                icon="el-icon-key"
                v-hasPermi="['coupon:coupon:enable']"
                >启用</el-dropdown-item
              >
              <el-dropdown-item
                command="handleEnd"
                icon="el-icon-key"
                v-hasPermi="['coupon:coupon:end']"
                >下架</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  getCouponList,
  deleteCouponById,
  suspendedCoupon,
  exportCoupon,
} from '@/api/coupon/manage.js'

export default {
  name: 'CouponManage',
  components: {},
  dicts: ['coupon_status', 'coupon_type', 'coupon_is_turn'],
  data() {
    return {
      showSearch: true,
      // 表格遮罩
      loading: false,
      // 表格数据
      seckillList: [],
      // 删除数据
      multiple: [],
      total: 1,
      // 导出遮罩层
      exportLoading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        couponName: undefined,
        couponStatus: undefined,
        category: undefined,
      },
    }
  },
  filters: {
    typeFilter(val, type) {
      if (val === null) return null
      let data = type.filter((item) => {
        return val == item.value
      })
      if (data.length) {
        return data[0].label
      } else {
        return val
      }
    },
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询租户管理列表 */
    getList() {
      this.loading = true
      getCouponList(this.queryParams).then((response) => {
        this.seckillList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.multiple = selection.map((item) => item.couponId)
    },
    /** 详情按钮操作 */
    handDetail(row) {
      this.$router.push({ path: '/coupon/manage/detail/' + row.couponId })
    },
    /** 操作事件 */
    handleCommand(val, row) {
      switch (val) {
        case 'handleSuspend':
          // 暂停
          this.handleSuspend(row, 3)
          break
        case 'handleEdit':
          // 编辑
          this.$router.push({ path: '/coupon/manage/edit/' + row.couponId })
          break
        case 'handleDelete':
          // 删除
          this.handleDelete(row)
          break
        case 'handleStart':
          // 上架
          this.handleSuspend(row, 1)
          break
        case 'appointBusinss':
          // 指定业务
          this.$router.push({ path: '/coupon/manage/appoint/' + row.couponId })
          break
        case 'handleOpen':
          //启用
          this.handleSuspend(row, 1)
          break
        case 'handleEnd':
          //下架
          this.handleSuspend(row, 4)
          break
        default:
          console.log('操作类别不正确')
      }
      console.log(val, row)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ path: '/coupon/manage/add' })
    },
    /** 暂停操作 */
    handleSuspend(row, couponStatus) {
      suspendedCoupon({
        couponId: row.couponId,
        couponStatus: couponStatus,
      }).then((res) => {
        this.$message.success(res.msg)
        this.getList()
      })
    },
    /** 删除操作 */
    handleDelete(row) {
      let message, ids
      if (row) {
        message = '是否确认删除优惠券名称为“ ' + row.couponName + ' ”的数据项？'
        ids = row.couponId
      } else {
        message = '是否确认删除优惠券数据项？'
        ids = this.multiple.join(',')
      }
      this.$modal.confirm(message).then((e) => {
        deleteCouponById({ id: ids }).then((res) => {
          this.$message.success(res.msg)
          this.getList()
        })
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = {
        couponName: this.queryParams.couponName,
        couponStatus: this.queryParams.couponStatus,
        category: this.queryParams.category,
      }
      this.$modal
        .confirm('是否确认导出优惠券数据项？')
        .then(() => {
          this.exportLoading = true
          exportCoupon(queryParams)
            .then((res) => {
              this.$download.name(res.msg)
              this.exportLoading = false
            })
            .catch((err) => {
              this.exportLoading = false
            })
        })
        .catch(() => {})
    },
  },
}
</script>
